extends Node
# 双指缩放参考FinepointCGI的教程 https://www.youtube.com/watch?v=vnAkooGCLDA
# 他的代码库 https://github.com/finepointcgi/Touch-Camera-Tutorial

const CAMERA_SPEED: float = 5

var touch_points: Dictionary = {}
var start_distance #手指间距离


@onready var camera_root = $"../CameraRoot"
@onready var spring_arm_3d = %SpringArm3D
signal handle_singal_drag_quest(event:InputEventScreenDrag)
signal adjust_camera(adjustment:float)


#下面这些变量都是2d相机本来应该有的
var camera_length:float:
	set(value):
		spring_arm_3d.distance = value
	get:
		return spring_arm_3d.distance

func _input(event):
	if event is InputEventScreenTouch:
		handle_touch(event)
	elif event is InputEventScreenDrag:
		handle_drag(event)
		
func handle_touch(event: InputEventScreenTouch):
	if event.pressed:
		touch_points[event.index] = event.position
	else:
		touch_points.erase(event.index)
	
	if touch_points.size() == 2:
		var touch_point_positions = touch_points.values()
		start_distance = touch_point_positions[0].distance_to(touch_point_positions[1])
		
func handle_drag(event: InputEventScreenDrag):
	touch_points[event.index] = event.position
	
	if touch_points.size() == 1:
		handle_singal_drag_quest.emit(event) # 直接转交任务
			
	elif touch_points.size() == 2:
		var touch_point_positions = touch_points.values()
		var current_dist = touch_point_positions[0].distance_to(touch_point_positions[1])
		adjust_camera.emit( -log(current_dist / start_distance) * CAMERA_SPEED )
		start_distance = current_dist # 这样防止重复计算
